﻿// Decompiled with JetBrains decompiler
// Type: LineWithArrows.LineArrow
// Assembly: LineWithArrows, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: D25B6BCE-6F53-4019-8F12-3DE5AC6FF1F0
// Assembly location: C:\Users\KasimGulcan\Desktop\Kosgeb\DynaCAD\Project\lib\LineWithArrows.dll

using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace LineWithArrows
{
  public class LineArrow : Shape
  {
    public static readonly DependencyProperty ArrowLengthProperty = DependencyProperty.Register("ArrowLenght", typeof (double), typeof (LineArrow), (PropertyMetadata) new FrameworkPropertyMetadata((object) 10.0, FrameworkPropertyMetadataOptions.AffectsMeasure));
    public static readonly DependencyProperty ArrowAngleProperty = DependencyProperty.Register("ArrowAngle", typeof (double), typeof (LineArrow), (PropertyMetadata) new FrameworkPropertyMetadata((object) 30.0, FrameworkPropertyMetadataOptions.AffectsMeasure));
    public static readonly DependencyProperty IsArrowClosedProperty = DependencyProperty.Register("IsArrowClosed", typeof (bool), typeof (LineArrow), (PropertyMetadata) new FrameworkPropertyMetadata((object) false, FrameworkPropertyMetadataOptions.AffectsMeasure));
    public static readonly DependencyProperty CapsProperty = DependencyProperty.Register("Caps", typeof (LineArrow.ArrowEnds), typeof (LineArrow), (PropertyMetadata) new FrameworkPropertyMetadata((object) LineArrow.ArrowEnds.Both, FrameworkPropertyMetadataOptions.AffectsMeasure));
    public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register("StartPoint", typeof (Point), typeof (LineArrow), (PropertyMetadata) new FrameworkPropertyMetadata((object) new Point(0.0, 0.0), FrameworkPropertyMetadataOptions.AffectsMeasure));
    public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register("EndPoint", typeof (Point), typeof (LineArrow), (PropertyMetadata) new FrameworkPropertyMetadata((object) new Point(0.0, 0.0), FrameworkPropertyMetadataOptions.AffectsMeasure));
    private PathGeometry PG;
    private PathFigure PF;
    private LineSegment LS;

    public double ArrowLength
    {
      get
      {
        return (double) this.GetValue(LineArrow.ArrowLengthProperty);
      }
      set
      {
        this.SetValue(LineArrow.ArrowLengthProperty, (object) value);
      }
    }

    public double ArrowAngle
    {
      get
      {
        return (double) this.GetValue(LineArrow.ArrowAngleProperty);
      }
      set
      {
        this.SetValue(LineArrow.ArrowAngleProperty, (object) value);
      }
    }

    public bool IsArrowClosed
    {
      get
      {
        return (bool) this.GetValue(LineArrow.IsArrowClosedProperty);
      }
      set
      {
        this.SetValue(LineArrow.IsArrowClosedProperty, (object) (bool) (value ? true : false));
      }
    }

    public LineArrow.ArrowEnds Caps
    {
      get
      {
        return (LineArrow.ArrowEnds) this.GetValue(LineArrow.CapsProperty);
      }
      set
      {
        this.SetValue(LineArrow.CapsProperty, (object) value);
      }
    }

    public Point StartPoint
    {
      get
      {
        return (Point) this.GetValue(LineArrow.StartPointProperty);
      }
      set
      {
        this.SetValue(LineArrow.StartPointProperty, (object) value);
      }
    }

    public Point EndPoint
    {
      get
      {
        return (Point) this.GetValue(LineArrow.EndPointProperty);
      }
      set
      {
        this.SetValue(LineArrow.EndPointProperty, (object) value);
      }
    }

    protected override Geometry DefiningGeometry
    {
      get
      {
        this.PG.Figures.Clear();
        this.PF.Segments.Clear();
        this.PG.Figures.Add(this.PF);
        this.PF.Segments.Add((PathSegment) this.LS);
        this.LS.Point = this.EndPoint;
        this.PF.StartPoint = this.StartPoint;
        this.CalculateArrowHeads();
        return (Geometry) this.PG;
      }
    }

    public LineArrow()
    {
      this.PG = new PathGeometry();
      this.PF = new PathFigure();
      this.LS = new LineSegment();
      this.Caps = LineArrow.ArrowEnds.Both;
    }

    private void CalculateArrowHeads()
    {
      if (this.Caps == LineArrow.ArrowEnds.End)
      {
        Matrix matrix = new Matrix();
        Vector vector1 = this.PF.StartPoint - this.LS.Point;
        vector1.Normalize();
        Vector vector2 = vector1 * this.ArrowLength;
        PathFigure pathFigure = new PathFigure();
        PolyLineSegment polyLineSegment = new PolyLineSegment();
        matrix.Rotate(this.ArrowAngle / 2.0);
        pathFigure.StartPoint = this.LS.Point + vector2 * matrix;
        polyLineSegment.Points.Add(this.LS.Point);
        matrix.Rotate(-this.ArrowAngle);
        polyLineSegment.Points.Add(this.LS.Point + vector2 * matrix);
        pathFigure.Segments.Add((PathSegment) polyLineSegment);
        if (this.IsArrowClosed)
        {
          pathFigure.IsClosed = true;
          pathFigure.IsFilled = true;
        }
        this.PG.Figures.Add(pathFigure);
      }
      else
      {
        Matrix matrix = new Matrix();
        Vector vector1 = this.LS.Point - this.PF.StartPoint;
        vector1.Normalize();
        Vector vector2 = vector1 * this.ArrowLength;
        PathFigure pathFigure1 = new PathFigure();
        PolyLineSegment polyLineSegment1 = new PolyLineSegment();
        matrix.Rotate(this.ArrowAngle / 2.0);
        pathFigure1.StartPoint = this.PF.StartPoint + vector2 * matrix;
        polyLineSegment1.Points.Add(this.PF.StartPoint);
        matrix.Rotate(-this.ArrowAngle);
        polyLineSegment1.Points.Add(this.PF.StartPoint + vector2 * matrix);
        pathFigure1.Segments.Add((PathSegment) polyLineSegment1);
        if (this.IsArrowClosed)
        {
          pathFigure1.IsClosed = true;
          pathFigure1.IsFilled = true;
        }
        this.PG.Figures.Add(pathFigure1);
        matrix = new Matrix();
        Vector vector3 = this.PF.StartPoint - this.LS.Point;
        vector3.Normalize();
        Vector vector4 = vector3 * this.ArrowLength;
        PathFigure pathFigure2 = new PathFigure();
        PolyLineSegment polyLineSegment2 = new PolyLineSegment();
        matrix.Rotate(this.ArrowAngle / 2.0);
        pathFigure2.StartPoint = this.LS.Point + vector4 * matrix;
        polyLineSegment2.Points.Add(this.LS.Point);
        matrix.Rotate(-this.ArrowAngle);
        polyLineSegment2.Points.Add(this.LS.Point + vector4 * matrix);
        pathFigure2.Segments.Add((PathSegment) polyLineSegment2);
        if (this.IsArrowClosed)
        {
          pathFigure2.IsClosed = true;
          pathFigure2.IsFilled = true;
        }
        this.PG.Figures.Add(pathFigure2);
      }
    }

    public enum ArrowEnds
    {
      End,
      Both,
    }
  }
}
